package cn.xyf.eduservice.service.impl;

import cn.xyf.eduservice.domain.EduSubject;
import cn.xyf.eduservice.domain.excel.SubjectData;
import cn.xyf.eduservice.domain.subject.OneSubject;
import cn.xyf.eduservice.domain.subject.TwoSubject;
import cn.xyf.eduservice.listener.SubjectExcelListener;
import cn.xyf.eduservice.mapper.EduSubjectMapper;
import cn.xyf.eduservice.service.EduSubjectService;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author xuyinfeng
 * @since 2021-04-14
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    // 添加课程分类
    @Override
    public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) {
        try {
            InputStream stream = file.getInputStream();
            EasyExcel.read(stream, SubjectData.class, new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public List<OneSubject> findAllSubject() {
        // 查询所有一级分类
        QueryWrapper<EduSubject> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("parent_id", 0);
        List<EduSubject> oneSubjects = baseMapper.selectList(wrapper1);

        QueryWrapper<EduSubject> wrapper2 = new QueryWrapper<>();
        wrapper2.ne("parent_id", 0);
        List<EduSubject> towSubjects = baseMapper.selectList(wrapper2);

        // 最终数据的容器
        ArrayList<OneSubject> arrayList = new ArrayList<>();

        for (int i = 0; i < oneSubjects.size(); i++) {
            EduSubject eduSubject = oneSubjects.get(i);

            OneSubject oneSubject = new OneSubject();
            // oneSubject.setId(eduSubject.getId());
            // oneSubject.setTitle(eduSubject.getTitle());


            //把eduSubject里面获取出来的值，放到oneSubject对象里面
            BeanUtils.copyProperties(eduSubject, oneSubject);
            System.out.println(eduSubject);
            System.out.println(oneSubject);
            arrayList.add(oneSubject);

            ArrayList<TwoSubject> arrayList1 = new ArrayList<>();

            for (int n = 0; n < towSubjects.size(); n++) {
                EduSubject tSubject = towSubjects.get(n);

                if (tSubject.getParentId().equals(eduSubject.getId())) {
                    TwoSubject twoSubject = new TwoSubject();

                    BeanUtils.copyProperties(tSubject, twoSubject);

                    arrayList1.add(twoSubject);
                }
            }
            oneSubject.setChildren(arrayList1);
        }

        return arrayList;
    }
}
